package com.polaris.live.ui.honor

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ImmersionBar
import com.hjq.shape.view.ShapeView
import com.polaris.live.R
import com.polaris.live.common.adapter.ViewPagerAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.JsonUtils
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.ActivityHonorBinding
import com.polaris.live.resp.back_resp.UserMedalAllResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.getIntOrString
import com.polaris.live.utils.getLongOrString
import com.polaris.live.viewmodel.HonorViewModel

/**
 * HonorActivity 勋章墙
 *
 * @author Created by 半仙 on 2024/4/20/020 11:06
 */
class HonorActivity : BaseVbActivity<HonorViewModel, ActivityHonorBinding>() {

    private val mInitPage by lazy { intent.getIntOrString("page", 0) }

    private val mUserId by lazy { intent.getLongOrString("userId", UserManager.getUserId()) }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .barColor(R.color.black)
            .statusBarDarkFont(false)
            .navigationBarDarkIcon(false)
            .titleBarMarginTop(mBinding.barView)
            .init()
        ViewUtils.setMargin(
            mBinding.viewPager2,
            bottomMargin = NavigationBarUtils.getNavigationBarHeight(this)
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.getHonor(mUserId)
        mViewModel.userMedalsLiveData.observe(this) { resultState ->
            parseState(resultState, {
                initTitle(it)
            })
        }
    }

    private fun initTitle(userMedalAllResp: UserMedalAllResp) {
        val tabs = mutableListOf<String>()
        tabs.add(StringUtils.getString(string.rich_level))
        tabs.add(StringUtils.getString(string.diamond_game))
        tabs.add(StringUtils.getString(string.activity_string))
        val list = mutableListOf<Fragment>()
        list.add(RichCharmHonorFragment().apply {
            arguments = bundleOf(
                "honor" to JsonUtils.toJson(userMedalAllResp.levelMedals),
                "userId" to mUserId
            )
        })
        list.add(RichCharmHonorFragment().apply {
            arguments = bundleOf(
                "honor_game" to JsonUtils.toJson(userMedalAllResp.gameMedals),
                "userId" to mUserId
            )
        })
        list.add(RichCharmHonorFragment().apply {
            arguments = bundleOf(
                "honor" to JsonUtils.toJson(userMedalAllResp.actMedals),
                "userId" to mUserId
            )
        })
        mBinding.viewPager2.adapter = ViewPagerAdapter(list, this)
        TabLayoutMediator(
            mBinding.tab, mBinding.viewPager2
        ) { tab: TabLayout.Tab, p: Int ->
            val customView =
                LayoutInflater.from(this).inflate(R.layout.layout_custom_tab, null, true)
            val tabText = customView.findViewById<TextView>(R.id.tv_custom_text)
            val vCustomLine = customView.findViewById<ShapeView>(R.id.v_custom_line)
            tabText.text = tabs[p]
            if (p == 0) {
                tabText.setTextColor(ColorUtils.getColor(R.color.white))
                vCustomLine.visibility = View.VISIBLE
            } else {
                tabText.setTextColor(ColorUtils.getColor(R.color.color_ffffff_30))
                vCustomLine.visibility = View.INVISIBLE
            }
            tab.customView = customView
        }.attach()

        mBinding.tab.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.customView?.findViewById<TextView>(R.id.tv_custom_text)
                    ?.setTextColor(ColorUtils.getColor(R.color.white))
                tab?.customView?.findViewById<ShapeView>(R.id.v_custom_line)?.visibility =
                    View.VISIBLE
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.customView?.findViewById<TextView>(R.id.tv_custom_text)
                    ?.setTextColor(ColorUtils.getColor(R.color.color_ffffff_30))
                tab?.customView?.findViewById<ShapeView>(R.id.v_custom_line)?.visibility =
                    View.INVISIBLE
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

        mBinding.viewPager2.setCurrentItem(mInitPage, true)
    }
}